package net.hvacapp.cmvrfforandroid.ui.project;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.mirror.android.common.util.StringUtils;

import net.hvacapp.cmvrfforandroid.R;
import net.hvacapp.cmvrfforandroid.adapter.BaseAdapterHelper;
import net.hvacapp.cmvrfforandroid.adapter.QuickAdapter;
import net.hvacapp.cmvrfforandroid.constant.Constant;
import net.hvacapp.cmvrfforandroid.http.BasicHttpClient;
import net.hvacapp.cmvrfforandroid.http.HttpCallbackHandle;
import net.hvacapp.cmvrfforandroid.http.HttpConstant;
import net.hvacapp.cmvrfforandroid.manager.AccountManager;
import net.hvacapp.cmvrfforandroid.ui.BaseActivity;
import net.hvacapp.cmvrfforandroid.ui.MainTabBarActivity;
import net.hvacapp.cmvrfforandroid.ui.project.model.GetRoomTypeGroupDataResp;
import net.hvacapp.cmvrfforandroid.ui.project.model.RoomData;
import net.hvacapp.cmvrfforandroid.ui.project.model.RoomTypeGroupData;
import net.hvacapp.cmvrfforandroid.utils.AnnotationUtil;
import net.hvacapp.cmvrfforandroid.widget.CellSeperateLineView;
import net.hvacapp.cmvrfforandroid.widget.GroupSelectionView;
import net.hvacapp.cmvrfforandroid.widget.ItemSelectionView;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;

@EActivity(R.layout.activity_project_new_room)
public class ProjectNewRoomActivity extends BaseActivity {

    @ViewById(R.id.tv_select)
    TextView mSelectTv;
    @ViewById(R.id.lv_templete)
    ListView mTempleteLv;
    @ViewById(R.id.cet_room)
    EditText mRoomCet;

    private QuickAdapter<RoomTypeGroupData> mQuickAdapter;



    /**
     * 页面加载完后执行
     */
    @AfterViews
    @Override
    protected void viewDidLoad() {
        super.viewDidLoad();
        initViews();
        initEvent();
    }

    /***
     * 初始化视图
     */
    private void initViews() {
//        mActionView.setActionInfo(ProjectManager.getInstance().getTotal(), ProjectManager.getInstance().getCurrent());
    }

    /**
     * 初始化
     */
    @Override
    public void initBoot() {
        setupToolbar(0, "新建房间", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(AnnotationUtil.get(MainTabBarActivity.class));
            }
        });
        if (mQuickAdapter == null){
            mQuickAdapter = new QuickAdapter<RoomTypeGroupData>(context, R.layout.group_selection_cell_item) {
                @Override
                protected void convert(final BaseAdapterHelper helper, final RoomTypeGroupData item) {
                    final GroupSelectionView view = helper.getView(R.id.group_type);
                    view.setTitleString(item.getGroup());
                    view.setInfoString("");
                    view.setGroupVisible(item.isExpand());
                    if (item.isExpand()) {
                        helper.setVisible(R.id.ll_content, true);
                    }
                    else {
                        helper.setVisible(R.id.ll_content, false);
                    }
                    view.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            item.setExpand(!item.isExpand());
                            view.setGroupVisible(item.isExpand());
                            if (item.isExpand()) {
                                helper.setVisible(R.id.ll_content, true);
                            }
                            else {
                                helper.setVisible(R.id.ll_content, false);
                            }
                        }
                    });

                    LinearLayout contentLl = helper.getView(R.id.ll_content);
                    for (final RoomData rItem : item.getRoomTypes()) {
                        CellSeperateLineView line = new CellSeperateLineView(ProjectNewRoomActivity.this);
                        contentLl.addView(line);

                        ItemSelectionView itemView = new ItemSelectionView(ProjectNewRoomActivity.this);
                        itemView.setTitleString(rItem.getName());
                        itemView.setItemSelected(false);
                        itemView.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                //选择了这个房间
                                Intent intent = new Intent();
                                Bundle bundle = new Bundle();
                                bundle.putParcelable("room", rItem);
                                intent.putExtras(bundle);
                                setResult(RESULT_OK, intent);
                                finish();
                            }
                        });
                        contentLl.addView(itemView);
                    }
                }
            };
        }

        mTempleteLv.setAdapter(mQuickAdapter);
    }


    /**
     * 初始化数据
     */
    @Override
    public void initData(Intent intent) {
        loadAllRooms();
    }



    /**
     * 页面名称
     */
    @Override
    public String getName() {
        return "";
    }

    private void initEvent() {
        mSelectTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (StringUtils.isEmpty(mRoomCet.getText().toString())) {
                    showMessage("房间名称不能为空！");
                }
                RoomData data = new RoomData();
                data.setName(mRoomCet.getText().toString());
                data.setGuidId(Constant.EMPTY_DEVICE_ID);

                Intent intent = new Intent();
                Bundle bundle = new Bundle();
                bundle.putParcelable("room", data);
                intent.putExtras(bundle);
                setResult(RESULT_OK, intent);
                finish();
            }
        });

    }


    private void loadAllRooms() {
        BasicHttpClient.httpGetAsync(HttpConstant.HTTP_GET_ALL_ROOMTYPES, AccountManager.getInstance().getToken(), new HttpCallbackHandle<GetRoomTypeGroupDataResp>() {
            @Override
            public void onSuccess(GetRoomTypeGroupDataResp obj) {
                // 成功
                mQuickAdapter.addAll(obj.getGetRoomTypesResult());
            }

            @Override
            public void onFailure(String response) {
                showMessage("获取房间模板失败，请重试！");
            }
        });
    }


}
